<?php

class Controller_Project extends Controller_Website {

    public function action_list() {
        $where = array();
        $where['status'] = Arr::get($_GET, 'status');
        $where['project_cid'] = Arr::get($_GET, 'project_cid');
        $where['profession_cid'] = Arr::get($_GET, 'profession_cid');
        $where['ORDER'] = 'id DESC';
        $where = array_filter($where, 'strlen');
        
        $project_name = Arr::get($_GET, 'project_name');
        if (!empty($project_name)) {
            $where['project_name'] = array('like'=>"%$project_name%");
        }
        
        $m_project = Model::factory('project');
        $total = $m_project->count($where);
        $pager = new Pager($total, 5);
        $list = $m_project->select($pager->offset, $pager->size, $where)->as_array();
        
        $m_projectCategory = Model::factory('projectCategory');
        $projectCategory = $m_projectCategory->getAll()->as_array();
        
        $m_professionCategory = Model::factory('professionCategory');
        $professionCategory = $m_professionCategory->getAll()->as_array();

        foreach ($list as &$item) {
            foreach ($projectCategory as &$p_item) {
            	if ($item['project_cid'] == $p_item['id']) {
            		$item['project_cid'] = $p_item['name'];
                }
            }
        }
//        print_r($list);exit;
        $this->content = View::factory('project/list');
        $this->content->list = $list;
        $this->content->pager = $pager;
        $this->content->projectCategory = $projectCategory;
        $this->content->professionCategory = $professionCategory;
    }
    

    public function action_add() {
        $m_project = Model::factory('project');
        
        if (!empty($_POST)) {
            $data = $this->_get_data($_POST);
            $data['project_begin'] = strtotime($_POST['project_begin']);
            $data['project_end'] = strtotime($_POST['project_end']);
            //拥有多个视频 ---- 存放到视频库里
            $video_arr = $data['video'];
            $video_ids= array();
            $m_video = Model::factory('video');
            
            foreach ($video_arr as $val){
                $res = $m_video->insert(array(
                'url'=>$val,
                'desc'=>$data['desc'],
                'pic'=>$data['pic'],
                'video_title'=>$data['project_name'],
                'add_time'=>time(),
                'edit_time'=>time()
            ));
                $video_ids[]= $res[0];
            }
            $data['video'] = json_encode($video_ids);
            $data['status'] = 2;
            $ret = $m_project->insert($data);
            if ($ret !== false) {
                $this->redirect('project/list');
            }
        }
        
        $m_projectCategory = Model::factory('projectCategory');
        $cat_list = $m_projectCategory->getAll(array('status'=>'open'))->as_array('id');
        $cat_tree = Category::get_children_tree($cat_list);
        
        $m_professionCategory = Model::factory('professionCategory');
        $cat_list2 = $m_professionCategory->getAll(array('status'=>'open'))->as_array('id');
        $cat_tree2 = Category::get_children_tree($cat_list2);
        
        $this->content = View::factory('project/add');
        $this->content->cat_list = $cat_list;
        $this->content->cat_tree = $cat_tree;
        $this->content->cat_list2 = $cat_list2;
        $this->content->cat_tree2 = $cat_tree2;
        $this->content->info = null;
    }

    public function action_edit() {
        $id = $_GET['id'];
        $m_project = Model::factory('project');
        
        if (!empty($_POST)) {
            
            $data = $this->_get_data($_POST);
            $data['project_begin'] = strtotime($_POST['project_begin']);
            $data['project_end'] = strtotime($_POST['project_end']);
//             var_dump($data);exit;
            //拥有多个视频 ---- 存放到视频库里
            $video_arr = $data['video'];
            $video_ids= array();
            $m_video = Model::factory('video');
            
            foreach ($video_arr as $val){
                $res = $m_video->insert(array(
                'url'=>$val,
                'desc'=>$data['desc'],
                'pic'=>$data['pic'],
                'video_title'=>$data['project_name'],
                'add_time'=>time(),
                'edit_time'=>time()
            ));
                $video_ids[]= $res[0];
            }
            $data['video'] = json_encode($video_ids);
            
            
            $ret = $m_project->updateById($data, $id);
            if ($ret !== false) {
                $this->redirect('project/list');
            }
        }

        $m_projectCategory = Model::factory('projectCategory');
        $cat_list = $m_projectCategory->getAll(array('status'=>'open'))->as_array('id');
        $cat_tree = Category::get_children_tree($cat_list);
        
//         var_dump($cat_tree);
        
        $m_professionCategory = Model::factory('professionCategory');
        $cat_list2 = $m_professionCategory->getAll(array('status'=>'open'))->as_array('id');
        $cat_tree2 = Category::get_children_tree($cat_list2);
        
        $info = $m_project->getRowById($id);
        $info['video'] = json_decode($info['video'],true);
        
        $m_video = Model::factory('video');
        $video_urls = array();
        for ($n=0; $n<count($info['video']); $n++) {
            $video_urls[] = $m_video->getRowById($info['video'][$n]);
        }
        $info['video'] = $video_urls;
//         var_dump($info);exit;
        
        $this->content = View::factory('project/edit');
        $this->content->cat_list = $cat_list;
        $this->content->cat_tree = $cat_tree;
        $this->content->cat_list2 = $cat_list2;
        $this->content->cat_tree2 = $cat_tree2;
        $this->content->info = $info;
    }

    public function action_del() {
        $id = $_GET['id'];
        $m_project = Model::factory('project');
        $ret = $m_project->deleteById($id);
        if ($ret !== false) {
            $this->redirect(Request::$referrer);
        }
    }
    
   /*  public function action_close() {
        $id = $_GET['id'];
        $data = array(
            'status' => 'close',
        );
        $m_project = Model::factory('project');
        $ret = $m_project->updateById($data, $id);
        if ($ret !== false) {
            $this->redirect(Request::$referrer);
        }
    }
    
    public function action_open() {
        $id = $_GET['id'];
        $data = array(
            'status' => 'open',
        );
        $m_project = Model::factory('project');
        $ret = $m_project->updateById($data, $id);
        if ($ret !== false) {
            $this->redirect(Request::$referrer);
        }
    } */
    
    public function action_close2() {
        $id = $_GET['id'];
        $data = array(
            'is_recommend' => 0,
        );
        $m_project = Model::factory('project');
        $ret = $m_project->updateById($data, $id);
        if ($ret !== false) {
            $this->redirect(Request::$referrer);
        }
    }
    
    public function action_open2() {
        $id = $_GET['id'];
        $data = array(
            'is_recommend' => 1,
        );
        $m_project = Model::factory('project');
        $ret = $m_project->updateById($data, $id);
        if ($ret !== false) {
            $this->redirect(Request::$referrer);
        }
    }
    
    public function action_addmember() {
        $project_id = Arr::get($_GET, 'id');
        $m_project = Model::factory('project');
        $project = $m_project->getRowById($project_id);
        
        $m_projectMember = Model::factory('projectMember');
        
        if (!empty($_POST)) {

            for ($n=0;$n<count($_POST['user_id']);$n++) {
                $data = $_POST;
                $data['user_id'] = $_POST['user_id'][$n];
                $data['person_cate_id'] = $_POST['person_cate_id'][$n];
                $data['remuneration'] = $_POST['remuneration'][$n];
                $data['real_remuneration'] = $_POST['real_remuneration'][$n];
                $data['project_id'] = $project_id;
                $data['project_name'] = $project['project_name'];
                $data['cate_id'] = $project['project_cid'];
                $ret = $m_projectMember->insert($data);
            }
            

            if ($ret !== false) {
                $this->redirect('project/list');
            }
  
        }
        
        $m_personCategory = Model::factory('personCategory');
        $cat_list = $m_personCategory->getAll(array('status'=>'open'))->as_array('id');
        $cat_tree = Category::get_children_tree($cat_list);
        
        $m_personInfo = Model::factory('personInfo');
        $personInfo = $m_personInfo->getAll()->as_array();
//         var_dump($personInfo);exit;
        
        
        $this->content = View::factory('project/addmember');
        $this->content->cat_list = $cat_list;
        $this->content->cat_tree = $cat_tree;
        $this->content->personInfo = $personInfo;
        $this->content->info = null;
    }
    
    
    
    public function action_adddevice() {
        $project_id = Arr::get($_GET, 'id');
        $m_projectDevice = Model::factory('projectdevice');
        
        $m_deviceInfo = Model::factory('deviceInfo');
        $deviceInfo = $m_deviceInfo->getAll()->as_array();
        
        if (!empty($_POST)) {
            
            for ($n=0;$n<count($_POST['device_id']);$n++) {
                $data = $_POST;
                $data['project_id'] = $project_id;
                $data['device_id'] = $_POST['device_id'][$n];
                $device = $m_deviceInfo->getRowById($data['device_id']);
                $data['device_name'] = $device['name'];
                $data['device_pic'] = $device['pic'];
                $data['price'] = $_POST['price'][$n];
                $data['s_time'] = strtotime($_POST['s_time'][$n]);
                $data['e_time'] = strtotime($_POST['e_time'][$n]);
                $ret = $m_projectDevice->insert($data);
            }
            

            if ($ret !== false) {
                $this->redirect('project/list');
            }
  
        }
    
        
        //         var_dump($personInfo);exit;
    
    
        $this->content = View::factory('project/adddevice');

        $this->content->deviceInfo = $deviceInfo;
        $this->content->info = null;
    }
    
    
    public function action_addplace() {
        $project_id = Arr::get($_GET, 'id');
        $m_projectPlace = Model::factory('projectplace');
    
        $m_placeInfo = Model::factory('placeInfo');
        $placeInfo = $m_placeInfo->getAll()->as_array();
        
        if (!empty($_POST)) {
            
            for ($n=0;$n<count($_POST['place_id']);$n++) {
                $data = $_POST;
                $data['project_id'] = $project_id;
                $data['place_id'] = $_POST['place_id'][$n];
                $place = $m_placeInfo->getRowById($data['place_id']);
                $data['place_item'] = json_encode($place);
                $data['price'] = $_POST['price'][$n];
                $data['s_time'] = strtotime($_POST['s_time'][$n]);
                $data['e_time'] = strtotime($_POST['e_time'][$n]);
                $data['create_time'] = time();
                $ret = $m_projectPlace->insert($data);
            }
    
    
            if ($ret !== false) {
                $this->redirect('project/list');
            }
    
        }
    
        
        //         var_dump($personInfo);exit;
    
    
        $this->content = View::factory('project/addplace');
        $this->content->placeInfo = $placeInfo;
        $this->content->info = null;
    }
    

    protected function _get_data($post) {
        $data = array_intersect_key($post, array_flip(array('project_name','project_cid','profession_cid','desc','director','pic','video','project_begin','project_end','author','city')));
        if (!empty($data['pic'])) {
            $data['pic'] = array_pop($data['pic']);
        }
        return $data;
    }
    
}

